import objectAssign from 'object-assign'
/**
 * 将加载的节点展开数据，与现有的数据组合，生成新的树节点的数据
 * @param treeData，现有数据
 * @param currentKey,当前加载数据的父节点
 * @param childData，当前加载的数据
 */
export function getNewTreeData(treeData, currentKey, childData) {
    const loop = data => {
        if (currentKey != "0") {
            data.map(item => {
                let {favid, child, children} = item;
                if (favid == currentKey) {
                    item.children = childData;
                } else {
                    if (child == "1" && !!children) {
                        loop(children);
                    }
                }
            });
        }
    }

    let ndata = [];
    loop(treeData, ndata);
    if (currentKey != "0") {
        ndata = [...treeData];
    } else {
        ndata = [...childData];
    }
    return ndata;
}

/**
 * 新增节点时的数据处理
 * @param treeData
 * @param selectedKey
 * @returns {[*]}
 */
export function getAddTreeData(treeData, selectedKey) {
    const loop = (data, nodeData) => {
        if (!selectedKey || selectedKey == "0") {     //没有选中任何节点，默认认为选中的是根节点，新增的目录在第一级
            data.push(nodeData);
        } else {
            data.map(item => {
                let {favid, child, children} = item;
                if (favid == selectedKey) {
                    if (!!item.children) {
                        item.children = [...item.children, nodeData];
                    } else {
                        item.child = "1";
                        item.children = [nodeData];
                    }
                } else {
                    if (child == "1" && !!children) {
                        loop(children, nodeData);
                    }
                }
            });
        }
    }

    let addNode = {favid: "-9", child: "0", favname: "新建目录", isinput: true, parentid: selectedKey};
    loop(treeData, addNode);
    return [...treeData];
}

/**
 * 编辑节点时的数据处理
 * @param treeData
 * @param nodeKey
 * @returns {[*]}
 */
export function getEditTreeData(treeData, nodeKey) {
    const loop = (data, key) => {
        data.map(item => {
            let {favid, child, children} = item;
            if (favid == key) {
                item.isinput = true;
            } else {
                if (child == "1" && !!children) {
                    loop(children, key);
                }
            }
        });
    }

    loop(treeData, nodeKey);
    return [...treeData];
}

/**
 * 更新节点信息
 * @param treeData
 * @param nodeKey
 * @param nodeData
 * @returns {[*]}
 */
export function updateNodeData(treeData, nodeKey, nodeData) {
    const loop = (data, key, node) => {
        data.map(item => {
            let {favid, child, children} = item;
            if (favid == nodeKey) {
                objectAssign(item, {...node});  //更新节点的信息
            } else {
                if (child == "1" && !!children) {
                    loop(children, key, node);
                }
            }
        });
    }

    loop(treeData, nodeKey, nodeData);
    return [...treeData];
}


/**
 * 鼠标移动到节点上，显示 编辑、删除 操作按钮
 * @param treeData
 * @param selectedKey
 * @returns {[*]}
 */
export function getOverTreeData(treeData, nodeKey, isover) {
    const loop = (data, key) => {
        data.map(item => {
            let {favid, child, children} = item;
            if (favid == nodeKey) {
                item.isover = isover;
            } else {
                if (child == "1" && !!children) {
                    loop(children, key);
                }
            }
        });
    }

    loop(treeData, nodeKey);
    return [...treeData];
}

/**
 * 根据key，获取节点信息
 * @param treeData
 * @param nodeKey
 * @returns {{}}
 */
export function getNodeData(treeData, nodeKey) {
    const loop = (data, key, ndata) => {
        for (let i = 0; i < data.length; i++) {
            let item = data[i];
            let {favid, child, children} = item;
            if (favid == key) {
                objectAssign(ndata, {...item});
                return;
            } else {
                if (child == "1" && !!children) {
                    loop(children, key, ndata);
                }
            }
        }
    }

    let nodeData = {};
    loop(treeData, nodeKey, nodeData);
    return nodeData;
}


export function stopEvent() {
    var event = window.event || arguments.callee.caller.arguments[0]
    if (!event) {
        return false;
    }
    if (event.stopPropagation) {
        event.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
    return false;
}